<template>
    <el-card shadow="never">
        <template #header>
            <el-alert effect="dark" :closable="false" title="使用说明">菜单规则包含两部分，(1)菜单(2)规则，菜单如果不存在子菜单，需要设置控制器与方法，表示点击菜单时访问位置。菜单仅提供显示功能，如果要设置访问位置的权限，需要继续配置规则。</el-alert>
        </template>
        <yun-table
                :columns="indexColumns"
                toolbar="refresh,add,edit,del,more"
                @render="onRender"
                ref="yuntable"
                :is-tree="true"
                :common-search="false"
                :pagination="false"
                :auth="auth"
                :extend="extend">
                <template #formatter="item">
                    <div v-if="item.field=='icon'">
                        <i :class="item.rows.icon"></i>
                    </div>
                </template>
        </yun-table>
    </el-card>
</template>
<script>
    import table from "@components/Table.js";
    export default{
        components:{'YunTable':table},
        data:{
            auth:{
                add:Yunqi.auth.check('app\\admin\\controller\\auth\\Rule','add'),
                edit:Yunqi.auth.check('app\\admin\\controller\\auth\\Rule','edit'),
                del:Yunqi.auth.check('app\\admin\\controller\\auth\\Rule','del'),
                multi:Yunqi.auth.check('app\\admin\\controller\\auth\\Rule','multi'),
                download:Yunqi.auth.check('app\\admin\\controller\\auth\\Rule','download'),
            },
            extend:{
                index_url: 'auth/rule/index',
                add_url: 'auth/rule/add',
                edit_url: 'auth/rule/edit',
                del_url: 'auth/rule/del',
                multi_url: 'auth/rule/multi',
                download_url: 'auth/rule/download',
            },
            indexColumns:[
                {checkbox: true,selectable:function (row,index){
                    if(!row.ismenu){
                        return false;
                    }
                    return true;
                }},
                {field: 'id',title: __('ID'),width:80},
                {field: 'title',expand:true,title: __('标题'),align:'left',formatter:function (data,row){
                    if(row.ismenu){
                        return data;
                    }else{
                        data=JSON.parse(data);
                        return data.join(',');
                    }
                }},
                {field: 'controller', title: __('控制器'),align:'left',formatter:function (data,row){
                    if(!data){
                        return '';
                    }
                    return data;
                }},
                {field: 'action', title: __('方法'),align:'left',formatter:function (data,row){
                    if(!data){
                        return '';
                    }
                    if(row.ismenu){
                        return data;
                    }else{
                        data=JSON.parse(data);
                        return data.join(',');
                    }
                }},
                {field: 'icon',width:80, title: __('图标'),formatter:Yunqi.formatter.slot},
                {field: 'ismenu',width:80, title: __('菜单'),formatter:function(data){
                    if(data==1){
                        return __('是');
                    }
                    return __('否');
                }},
                {field: 'isplatform', title: __('平台'),width:80,formatter: function(data,row){
                    if(row.pid===0 && row.ismenu){
                        let t=Yunqi.formatter.switch;
                        t.value=data;
                        return t;
                    }
                }},
                {field: 'weigh', title: __('权重'),width:80},
                {field: 'status', title: __('状态'),width:80,searchList: {'normal': __('正常'),'hidden': __('隐藏')},formatter: Yunqi.formatter.switch},
                {treeExpand: true},
                {
                    field: 'operate',
                    title: __('操作'),
                    width:150,
                    action:{sort:true,edit:true, del:true}
                }
            ]
        },
        methods: {
            onRender:function (data){
                if(Yunqi.config.action=='edit'){
                    if(parseInt(data.ismenu)===0){
                        let title=JSON.parse(data.title);
                        let action=JSON.parse(data.action);
                        let actions={};
                        for(let i=0;i<title.length;i++){
                            actions[action[i]]=title[i];
                        }
                        data.actions=actions;
                        this.changeMenu(0);
                    }
                }
            }
        }
    }
</script>
<style>

</style>